import screens.screen
import pygame.display
from pygame.locals import *
from pygameExt import pygameext
import os
from data import items

class moveScreen(screens.screen.screeninherit):
    def __init__(self, screen, items=[], background=""):
        self.items=items
        self.background=background
        self.nextscene=0
        self.square = 5
        self.screen = screen
    
    def init(self):
        self.screen.fill((0,0,0))
        pygame.display.update()
        #assignControls(self.itempos)
        self.keylisteners = [screens.screen.keyListener([items.heidi,items.heibert], 'up',self.keyup), 
                        screens.screen.keyListener([items.heidi,items.heibert], 'down',self.keydown),
                        screens.screen.keyListener([items.heidi,items.heibert], 'left',self.keyleft),
                        screens.screen.keyListener([items.heidi,items.heibert], 'right',self.keyright)]
        if (self.background!=""):
            self.background=pygame.image.load(os.path.join('../data/images', self.background))
        else:
            self.screen.fill((0,0,0))
        self.drawstuff(self.items)
        
    def keyup(self, character):
        self.key(character,-1,0)
    def keydown(self, character):
        self.key(character,1,0)
    def keyleft(self, character):
        self.key(character,0,-1)
    def keyright(self, character):
        self.key(character,0,+1)
        
    def key(self, character, npositiony, npositionx):
        character = self.getCharacter(character)
        #if (self.onsomething((character.positionx+npositionx,character.positiony+npositiony)) == False):
        character.positionx = character.positionx + npositionx*character.movelength
        character.positiony = character.positiony + npositiony*character.movelength
        self.replace(self.items, character)
        self.drawstuff(self.items)
        #else:
        #    print "you hit something!!!"
    def replace(self,items,character):
        for char in items:
            if char.character.name == character.character.name:
                char = character
        
    def getCharacter(self, character):
        for item in self.items:
            if item.character == character:
                return item
            
    def onsomething(self, position):
        return False        
        #for item in self.items:
        #    print "checking an item"
        #    print item.pos
        #    if (item.pos==position):
        #        return True
    
    def drawstuff(self,items):
        self.screen.fill((0,0,0))
        for item in items:
            if (item != 0):
                self.screen.blit(item.character.imageTop, (item.positionx,item.positiony))
            pygame.display.update()
                
    def loop(self):
        return